package com.leetcode;

public class 盛最多水的容器 {
    public static void main(String[] args) {
        int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        System.out.println(maxArea(height));
    }
    public static int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int maxArea = 0;
        while (left < right) {
            int width = right - left;
            if (height[left] < height[right]) {
                maxArea = Math.max(maxArea, width * height[left]);
                left++;
            } else {
                maxArea = Math.max(maxArea, width * height[right]);
                right--;
            }
        }
        return maxArea;
    }

    /**
     * @description: 复习自写
     * @author: lei
     * @date: 2024/3/9 22:55
     * @param: [height]
     * @return: int
     **/
    public static int maxArea02(int[] height) {
        int start = 0, end = height.length - 1;
        int area = 0;
        while (start < end) {
            int endNum = height[end];
            int startNum = height[start];
            int high = Math.min(startNum, endNum);
            int weigh = end - start;
            area = Math.max(area, high * weigh);
            //固定开始的一端，往左移动结束的一段
            for (int i = end - 1; i > start; i--) {
                if (height[i] > endNum) {
                    high = Math.min(startNum, height[i]);
                    weigh = i - start;
                    int tempArea = high * weigh;
                    area = Math.max(area, tempArea);
                }
                if (high == 1 || high == 0 || height[i] > startNum) break;
            }
            start++;
        }
        return area;
    }
}
